<?php

/**
 * @author: biggege
 * @since:  2021-08-20 10:36:53
 */

namespace App\Validations\Manage;

use App\Helpers\ManageValidator;
use App\Models\Role;
use Validator;

trait ContentValidator
{
    use ManageValidator;

    protected $validatorAttributes = [
        'create_admin_id' => '创建人ID',
        'content_title' => '标题',
        'content_month_age' => '适合儿童月龄',
        'content_preview_image' => '内容预览图',
        'content_video' => '视频',
        'content_video_path' => '视频链接地址',
        'content_summary' => '文章简述',
        'content_detail' => '详情',
        'content_status' => '发布状态',
        'content_sort' => '权重',
        'content_category' => '分类',
        'content_outside_link' => '站外链接',
        'content_type' => '文章类别',
    ];

    // 更新
    private function updateValidation(array $data) {

        $validator = Validator::make($data, [
            'content_title' => 'required|string|min:1|max:100',
            'content_summary' => 'required|string|min:1|max:500',
            'content_month_age' => 'sometimes|nullable|integer',
            'content_preview_image' => 'sometimes|nullable|string|min:1|max:128',
            'content_video' => 'sometimes|nullable|string|min:1|max:128',
            'content_video_path' => 'sometimes|nullable|string|min:1|max:255',
            'content_detail' => 'required_if:content_type,1|nullable|string|min:1|max:100000',
            'content_status' => 'required|integer|min:1|max:3',
            'content_category' => 'required|integer|min:1|max:3',
            'content_sort' => 'sometimes|nullable|integer',
            'content_type' => 'required|integer|min:1|max:2',
            'content_outside_link' => 'required_if:content_type,2|nullable|string|max:500',
        ], $this->validatorMessage, $this->validatorAttributes);

        if ($validator->fails()) {
            $errors = $validator->errors()->first(); // all()
            if(strpos($errors, 'content_outside_link') != false) {
                $errors = '站外链接不能为空';
            }
            return $this->error(400, $errors); // implode(' | ', $errors)
        }
    }













}